<?php
namespace app\api\controller;
use app\common\controller\Api;
use think\Request;
use think\Db;
/**
 * 购物车
 */
class Cars extends Api{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [''];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * @return  分类相关
     */
    public function getallcate(){

        $pid=$this->request->param('pid',0,'intval');

        $lists=db('catefrogood')->where('pid','eq',$pid)->order('weigh desc')->select();
        foreach ($lists as $k=>&$v){
            $v['image']=cdnurl($v['image'],true);
        }

        $this->success('分类信息',$lists);

    }

    /**
     * 获取产品信息
     */

    public function getgoodbypid(){

        $pid=$this->request->param('pid',0,'intval');
        $keyword=$this->request->param('key','','trim');

        $cate=db('catefrogood')->where('id','eq',$pid)->find();

        $lists=db('catefrolist')->where('catefrogood_id','eq',$pid)
            ->where('name','like','%'.$keyword.'%')
            ->select();

        foreach ($lists as $k=>&$v){

            $v['images']=getallimages($v['images']);

        }

        $this->success('获取产品信息',['title'=>$cate['name'],'lists'=>$lists]);


    }

    /**
     * 购物车数量
     */
    public function getcarcount(){

        $uid=$this->auth->id;

        $count=Db::name('shoppingcarts')->where('user_id','eq',$uid)->count();

        $this->success('购物车数量',$count);

    }

    /**
     * 产品详情
     */
    public function getgoodsinfobyid(){


        $id=$this->request->param('id',0,'intval');

        $goods=db('catefrolist')->where('id','eq',$id)->find();

        if (!$goods){
            $this->error('产品不存在');
        }

        $goods['images']=getallimages($goods['images']);

        $goods['pdf']=cdnurl($goods['pdf'],true);

        $goods['types']=explode(',',$goods['types']);

        $goods['size']=explode(',',$goods['size']);


        $this->success('产品详情',$goods);
    }

    /**
     * 添加购物车
     */
    public function addcar(){
        $uid=$this->auth->id;
        $id=$this->request->param('id',0,'intval');
        $type=$this->request->param('type','','trim');
        $size=$this->request->param('size','','trim');

        $goods=Db::name('catefrolist')->where('id','eq',$id)->find();
        if (!$goods){
            $this->error('产品不存在');
        }


        $car=Db::name('shoppingcarts')->where('user_id','eq',$uid)->where('catefrolist_id','eq',$id)->where('type','eq',$type)->find();

        if ($car){
            $this->error('已经添加过了');
        }



        $data['user_id']=$uid;
        $data['catefrolist_id']=$id;
        $data['type']=$type;
        $data['size']=$size;
        $data['desc']=$goods['desc'];
        $data['price']=$goods['money'];

        $data['catetime']=time();

        Db::name('shoppingcarts')->insert($data);

        $this->success('添加成功');

    }

    /**
    * 购物车全部商品
     */

    public function getallgoodforcar(){

        $uid=$this->auth->id;

        $lists=Db::name('shoppingcarts')->alias('a')
            ->join('catefrolist b','a.catefrolist_id=b.id')
            ->where('a.user_id','eq',$uid)
            ->field('b.*,a.size,a.type,a.price,a.id as carsid,a.desc')
            ->select();

        $money=0;

        foreach ($lists as $k=>&$v){
            $v['images']=getallimages($v['images']);
            $money+=$v['price'];
        }

        $this->success('购物车全部商品',['lists'=>$lists,'money'=>$money]);


    }


    /**
     * 删除购物车里的产品  报价单
     */
    public function deletebycar(){
        $id=$this->request->param('id',0,'intval');

        $res=Db::name('shoppingcarts')->where('id','eq',$id)->delete();
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }

    }



    /**
     * 修改购物车的金额
     */
    public function setcarmoney(){
        $id=$this->request->param('id',0,'intval');
        $money=$this->request->param('val',0,'intval');
        $res=Db::name('shoppingcarts')
            ->where('id','eq',$id)
            ->update(['price'=>$money]);
        if($res){
            $this->success('修改成功');
        }else   {
            $this->error('修改失败');
        }
    }

    /**
     * 创建报价单 然后 清空购物车
     */
    public function addquoted(){

        $uid=$this->auth->id;
        $merchid=$this->request->param('merchid',0,'intval');
        $merchname=$this->request->param('merchname',0,'trim');

        // 查询购物车里的全部产品
        $lists=Db::name('shoppingcarts')->alias('a')
            ->join('catefrolist b','a.catefrolist_id=b.id')
            ->where('a.user_id','eq',$uid)
            ->field('b.*,a.size,a.type,a.price,a.price as carsid,a.images,a.icons,a.desc')
            ->select();

        if (!$lists){
            $this->error('购物车为空');
        }

        //cmf_quotation

        $data['order']='BJ'.date('Ymd').rand(10,99);
        $data['merch_id']=$merchid;
        $data['addtime']=time();
        $data['savetime']=time();
        $data['user_id']=$uid;

        $carid= Db::name('quotation')->insertGetId($data);

        foreach ($lists as $v){
            $arr=[];

            $arr['quotation_id']=$carid;
            $arr['catefrolist_id']=$v['id'];
            $arr['size']=$v['size'];
            $arr['type']=$v['type'];
            $arr['catefrolist_name']=$v['name'];
            $arr['money']=$v['price'];
            $arr['images']=$v['images'];
            $arr['icons']=$v['icons'];
            $arr['desc']=$v['desc'];
            $arr['seesyatus']=$v['seesyatus'];


            Db::name('quotaion_log')->insert($arr);

        }

//            清空购物车
        Db::name('shoppingcarts')
            ->where('user_id','eq',$uid)
            ->delete();


        $this->success('操作成功');

    }


    /**
     * 获取我的报价单
     */

    public function getmyquoted(){

        $uid=$this->auth->id;

        $strtime=$this->request->param('strtime',0,'intval');
        $endtime=$this->request->param('endtime',0,'intval');
        $merch_id=$this->request->param('merch_id',0,'intval');

        // ... existing code ...
        $lists = Db::name('quotation')
            ->alias('q') // 为 quotation 表设置别名 q
            ->join('cmf_aa_merch m', 'q.merch_id = m.id') // 关联 cmf_aa_merch 表
//            ->where('q.user_id', '=', $uid)
            ->field('q.*,m.title');

        if ($strtime && $endtime) {
            // 添加时间区间筛选
            $lists = $lists->whereBetween('q.addtime', [$strtime, $endtime]);
        }

        if ($merch_id) {
            $lists = $lists->where('q.merch_id', '=', $merch_id);
        }

        $lists = $lists->select();

        foreach ($lists as $k => $v) {
            $lists[$k]['addtimea'] = date('y.m.d ', $v['addtime']);
            $lists[$k]['addtimeb'] = date('A h:i', $v['addtime']);



            $lists[$k]['savetimea'] = date('y.m.d ', $v['savetime']);
            $lists[$k]['savetimeb'] = date('A h:i', $v['savetime']);

        }
        // ... existing code ...


        if (!$lists){
            $this->error('购物车为空');
        }
        $this->success('操作成功',$lists);
    }

    /**
     * 获取我的历史报价单
     *  cmf_catefrolist_history
     */
    public function getmyhistoryquoted(){
        $id=$this->request->param('id',0,'intval');


//        $lists=Db::name('quotaion_log')->alias('a')
//            ->join('cmf_quotation b','a.quotation_id=b.id')
//            ->join('cmf_aa_merch c','b.merch_id=c.id')
//            ->where('a.catefrolist_id','eq',$id)
//            ->field('a.*,b.addtime,c.title')
//            ->limit(5)
//            ->select();

        $lists=Db::name('catefrolist_history')->where('catefrolist_id','eq',$id)
            ->order('addtime','desc')
            ->select();


        foreach ($lists as $k=>&$v){
            $v['addtime']=date('Y-m-d',$v['addtime']);
        }

        $this->success('操作成功',$lists);

    }

    /**
     * 修改价格
     */

    public function setcarmoneybyid(){

        $id=$this->request->param('id',0,'intval');
        $money=$this->request->param('money',0,'intval');
        $desc=$this->request->param('desc',0,'trim');
        $merch_id=$this->request->param('merch_id',0,'intval');



        $find=Db::name('shoppingcarts')->where('id','eq',$id)
            ->find();

        $res=Db::name('shoppingcarts')
            ->where('id','eq',$id)
            ->update(['price'=>$money]);


        if($res){

            //添加一个历史记录

            $merch=Db::name('aa_merch')->where(['id'=>$merch_id])->find();


            $arr['catefrolist_id']=$find['catefrolist_id'];
            $arr['moneya']=$find['price'];
            $arr['moneyb']=$money;
            $arr['desc']=$desc;
            $arr['user_id']=$this->auth->id;
            $arr['addtime']=time();
            $arr['nickname']=$this->auth->getUser()->nickname;
            $arr['merch_id']=$merch_id;
            $arr['merch_name']=$merch['title'];


            Db::name('catefrolist_history')->insert($arr);



            $this->success('修改成功');
        }else   {
            $this->error('修改失败');
        }

    }

    /**
     * 修改备注信息
     * desc,carimg,carid,caricon
     */
    public function setcargooddesc(){

        $id=$this->request->param('carid',0,'intval');
        $desc=$this->request->param('desc',0,'trim');
        $carimg=$this->request->param('carimg',0,'trim');
        $caricon=$this->request->param('caricon',0,'trim');


        $caricon=trim($caricon,',');

        $ret=Db::name('shoppingcarts')
            ->where('id','eq',$id)
            ->update(['desc'=>$desc,'images'=>$carimg,'icons'=>$caricon]);

        if ($ret){
            $this->success('修改成功');
        }

        $this->error('修改失败');


    }

    /**
     * 添加一个备注
     */

    public function addcardesc(){
        // 从请求参数中获取购物车记录的 ID
        $id = $this->request->param('id', 0, 'intval');
        // 从请求参数中获取备注信息
        $desc = $this->request->param('desc', '', 'trim');

        if (!$id || empty($desc)) {
            $this->error('购物车记录 ID 和备注信息不能为空');
        }

        // 更新购物车记录的备注信息
        $res = Db::name('quotation')->where('id', '=', $id)->update(['desc' => $desc]);

        if ($res) {
            $this->success('备注添加成功');
        } else {
            $this->error('备注添加失败');
        }
    }

    /**
     * 购物车详情
     *  id
     */
    public function getgoodforcarbyid(){

        $id=$this->request->param('id',0,'intval');

        $lists=Db::name('shoppingcarts')
            ->where('id','eq',$id)
            ->find();

        $lists['images']=getallimages($lists['images']);


        $list=Db::name('desc_icons')->order('weigh desc')->select();

        foreach ($list as $k=>&$v){

            if(str_contains($lists['icons'],$v['name'])){
                $list[$k]['status']=1;
            }
        }


        $this->success('操作成功',['lists'=>$lists,'icon'=>$list]);
    }

    /**
     * 删除一个信息 cmf_quotation*
    .*/
     public function delgoodforcarbyid(){


         $id=$this->request->param('id',0,'intval');


         $find=Db::name('quotation')
             ->where('id','eq',$id)
             ->find();

         if ($find['lcok']==1) {
             $this->error('已经锁定');

         }

             $res=Db::name('quotation')
             ->where('id','eq',$id)
             ->delete();

         Db::name('quotaion_log')->where(['quotation_id'=>$id])->delete();

         if($res){
             $this->success('删除成功');
         }else{
             $this->error('删除失败');
         }



     }


     //锁定信息
    public function lockgoodforcarbyid(){
        $id=$this->request->param('id',0,'intval');

        Db::name('quotation')
            ->where('id','eq',$id)
            ->update(['lcok'=>1]);

        $this->success('操作成功');
    }




}